﻿using FastCloud.Base.Config;
using FastCloud.Base.Desgin;
using FastCloud.DMP.Entity;
using FastIOC.Attribute;
using FastUtil.Convert;
using FastUtil.Data;
using System;
using System.Collections.Generic;
using System.Text;

namespace FastCloud.DMP.DB
{
    public class DB_CodeMain : BaseCacheML<Frame_CodeMain>
    {
        [AutoWired]
        private DB_CodeItem DB_CodeItem;

        public override void Insert(Frame_CodeMain Entity)
        {
            if (string.IsNullOrEmpty(Entity.CodeGuid))
            {
                Entity.CodeGuid = GenerateUtil.GenerateUnitGuid();
            }
            Entity.AddDate = DateTime.Now;
            base.Insert(Entity);
        }

        public override void DeleteByID(string UnitGuid)
        {
            base.DeleteByID(UnitGuid);
            DB_CodeItem.DeleteByMainGuid(UnitGuid);

        }

        public List<Frame_CodeMain> SelectAll()
        {
            return DTO.Select_Cache<Frame_CodeMain>(); ;
        }

        public Page SelectPage(string CodeName, string GroupGuid)
        {
            int PageIndex = ValueConvert.ConvertToInt32(CloudUtil.GetFormParam("PageIndex"));
            int PageSize = ValueConvert.ConvertToInt32(CloudUtil.GetFormParam("PageSize"));
            List<Frame_CodeMain> entityList = DTO.Select_Cache<Frame_CodeMain>();
            int total = entityList.Count;
            if (!string.IsNullOrEmpty(CodeName))
            {
                entityList = entityList.FindAll((Frame_CodeMain item) => item.CodeName.Contains(CodeName));
            }
            if (!string.IsNullOrEmpty(GroupGuid) && GroupGuid != "Top")
            {
                entityList = entityList.FindAll((Frame_CodeMain item) => item.GroupGuid == GroupGuid);
            }
            entityList.Sort((x, y) => -x.SortNum.CompareTo(y.SortNum));
            List<Frame_CodeMain> resultList = new List<Frame_CodeMain>();
            for (int i = (PageIndex - 1) * PageSize; i < entityList.Count && i < PageIndex * PageSize; i++)
            {
                resultList.Add(entityList[i]);
            }
            Page page = new Page();
            page.Rows = resultList;
            page.Total = total;
            return page;
        }

        public bool ExistCodeMain(string CodeName)
        {
            List<Frame_CodeMain> entityList = DTO.Select_Cache<Frame_CodeMain>();
            entityList = entityList.FindAll((Frame_CodeMain item) => item.CodeName == CodeName);
            if (entityList.Count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool ExistCodeMain(string CodeGuid, string CodeName)
        {
            List<Frame_CodeMain> entityList = DTO.Select_Cache<Frame_CodeMain>();
            entityList = entityList.FindAll((Frame_CodeMain item) => item.CodeName == CodeName && item.CodeGuid != CodeGuid);
            if (entityList.Count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

    }
}
